﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace DarwinIsDead.Traits
{
    public class Frenzy : Trait
    {
        Player player;
        private int healthBonus;

        private float scaleMe;
        private float rotateMe;
        private SoundEffect nearSound;

        private Texture2D sprite;
        private SpriteFont debugFont;

        bool collide;

        // Ctor
        public Frenzy(Zombie owner, World world = null)
            : base(owner, world)
        {
            // Load sound, graphics, etc.
            sprite = World.Game.Content.Load<Texture2D>("Sprites\\Traits\\tractor");
            debugFont = World.Game.Content.Load<SpriteFont>("Fonts\\Courier New");
            nearSound = World.Game.Content.Load<SoundEffect>("Sounds\\roar");


            collide = false;
            healthBonus = 5;  // Adds 5 health to this zombie
            scaleMe = 1;
            rotateMe = 1;
        }

        public override object Clone()
        {
            Frenzy ret = new Frenzy(Owner, World);
            return ret;
        }

        public override void Initialize()
        {
            Owner.HitBox.Width += sprite.Bounds.Width;   // Add some dimension to the zombie's hitbox
            Owner.HitBox.Height += sprite.Bounds.Height;

            List<Entity> players = World.GetEntities<Player>();
            if (players.Count > 0)
            {
                player = (Player)players[0];
            }

            Level = 1;
        }

        public override void Collision(Entity other, Rectangle overlap)
        {
            if (other != null && other is Player)
            {
                Player player = (Player)other;
                player.Health -= 1;
                Vector2 ppos = new Vector2(player.Position.X - 10, player.Position.Y - 10);

                player.SetPosition(ppos);

                scaleMe *= (float)1.01;

                //Increase size evertime player hits 
                float temp1 = (float) sprite.Bounds.Width * scaleMe;
                float temp2 = (float) sprite.Bounds.Height * scaleMe;
                                
                Owner.HitBox.Width  = sprite.Bounds.Width + (int)(temp1 - sprite.Bounds.Width);
                Owner.HitBox.Height = sprite.Bounds.Height + (int)(temp1 - sprite.Bounds.Height);

                Random x = new Random();
                rotateMe = (float)(x.NextDouble());
                nearSound.Play();

                collide = true;
             }

             if (other is Bullet)
             {
                Bullet bullet = (Bullet)other;
                if (bullet.Friendly)
                {
                    collide = true;
                }
            }

        }

        protected override void AdjustLevel(int level)
        {
            // Apply any changes that occur with leveling up.
            scaleMe += (float) 0.01;

            int oldMaxHealth = Owner.MaxHealth;
            Owner.MaxHealth += level * healthBonus;
            
            // Scale the remaining health to the new cap
            Owner.Health = (int)(((float)Owner.Health / oldMaxHealth) *
                Owner.MaxHealth);
        }

        protected override void InternalDraw(SpriteBatch spriteBatch, Color color)
        {
            Vector2 drawPositon = new Vector2(Owner.Position.X, Owner.Position.Y);

            if (collide)
            {
                spriteBatch.Draw(sprite, drawPositon, null, Color.Red, rotateMe, Vector2.Zero, scaleMe, SpriteEffects.None, 0);
                collide = false;
            }
            else
            {
                spriteBatch.Draw(sprite, drawPositon, null, Color.White, rotateMe, Vector2.Zero, scaleMe, SpriteEffects.None, 0);
            }
        }
    }
}
